package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constants.RabbitMqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/12 2:00
 */
@Configuration
public class OrderMqConfig {
    //order服务交换机
    @Bean
    public Exchange orderEventExchange(){
        //String name, 交换机名称
        // boolean durable, 是否持久化
        // boolean autoDelete, 是否自动删除
        // Map<String, Object> arguments
        return new TopicExchange(RabbitMqConst.ORDER_EVENT_EXCHANGE,true,false,null);
    }
    @Bean
    //延迟队列
    public Queue orderDelayQueue(){
        //String name, 队列名称
        // boolean durable, 可持久化
        // boolean exclusive, 是否排他
        // boolean autoDelete, 是否自动删除
        // @Nullable Map<String, Object> arguments
        Map<String,Object> arguments=new HashMap<>();
        //消息过期,传输到死信交换机
        arguments.put("x-dead-letter-exchange",RabbitMqConst.ORDER_EVENT_EXCHANGE);
        //绑定死信交换机的路由key
        arguments.put("x-dead-letter-routing-key",RabbitMqConst.ORDER_RK_TIMEOUT);
        arguments.put("x-message-ttl",60000);
        return new Queue(RabbitMqConst.ORDER_DELAY_QUEUE,true,false,false,arguments);
    }
    //交换机和队列的绑定关系
    @Bean
    public Binding orderCreateBing(){
        // String destination,绑定目的地
        // DestinationType,绑定类型
        // String exchange, 交换机
        // String routingKey, 路由key
        // @Nullable Map<String, Object> arguments
        return new Binding(RabbitMqConst.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE,
                RabbitMqConst.ORDER_EVENT_EXCHANGE,RabbitMqConst.ORDER_RK_CREATE,
                null
                );
    }

    //死信队列  关单服务监听这个队列
    @Bean
    public Queue orderDeadQueue(){

        return new Queue(RabbitMqConst.ORDER_DEAD_QUEUE,true,false,false);
    }
    //交换机和队列绑定关系(每个消息传递途中用相同路由key)
    @Bean
    public Binding orderDeadBing(){
        return new Binding(RabbitMqConst.ORDER_DEAD_QUEUE, Binding.DestinationType.QUEUE,
                RabbitMqConst.ORDER_EVENT_EXCHANGE,RabbitMqConst.ORDER_RK_TIMEOUT,null);

    }

    //已支付队列
    @Bean
    public Queue orderPaidQueue(){
        return new Queue(RabbitMqConst.ORDER_PAID_QUEUE,true,false,false);
    }
    @Bean
    public Binding orderPaidBing(){
        return new Binding(RabbitMqConst.ORDER_PAID_QUEUE, Binding.DestinationType.QUEUE,
                RabbitMqConst.ORDER_EVENT_EXCHANGE,RabbitMqConst.ORDER_RK_PAID,null);
    }
}
